package csu.rjxyg.action.userAction;



import csu.rjxyg.action.BaseAction;
import csu.rjxyg.action.constant.PermissionConstant;
import csu.rjxyg.bean.DBObject;
import csu.rjxyg.bean.core.BaseObject;
import csu.rjxyg.action.constant.UserConstant;
import csu.rjxyg.factory.core.BeanFactory;
import csu.rjxyg.factory.core.UtilFactory;
import csu.rjxyg.service.BaseUserService;
import csu.rjxyg.util.LogUtil;
import csu.rjxyg.util.TimeUtil;

public class LoginAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private LogUtil log;	
	private TimeUtil time;
	
	private String userId;
	private String userPass;
	private BaseObject userBean;
	
	public LoginAction(){
		log  = UtilFactory.getLogUtil(getClass());
		time = UtilFactory.getTimeUtil();
	}
	
	public String login(){
		try{

			
			//当后期存在帐号后可删除
			if(userId.equals("root")  && userPass.equals("root")){
				userBean = new DBObject();
				userBean.set(UserConstant.USER_ID, userId);
				userBean.set(UserConstant.USER_NAME, "最高管理员");
				userBean.set(UserConstant.USER_LOGIN_QX, PermissionConstant.PERMISSION_TOP_ROOT);
				this.getSession().put(UserConstant.USER_SESSION, userBean);
				String temp = (String) getPermissionMap().get(PermissionConstant.PERMISSION_TOP_ROOT);
				log.info(userId + "登陆系统成功,登陆页面为:" + temp );
				return temp;
			}
			BaseUserService userService = (BaseUserService) factory.getBean(BaseAction.USER_SERVICE);
			userBean = userService.login(userId, userPass);
			
			if( userBean != null ){

				this.getSession().put(UserConstant.USER_SESSION, userBean);
				userBean.set(UserConstant.USER_LOGIN_TIME, time.getNowTime());
				userService.updateUserBean(userId, userBean);
				log.info(userId + "登陆系统成功" );
				String userLoginPermission = (String) userBean.get(UserConstant.USER_LOGIN_QX);
				String temp = (String) getPermissionMap().get(userLoginPermission);
				log.info(userId + "登陆系统成功,登陆页面为:" + temp );
				return temp;
			}
				
		}catch(Exception e){
			e.printStackTrace();
			log.info(e.getMessage());
		}
		log.info(userId + "登陆系统失败" );
		return this.FAIL;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getUserPass() {
		return userPass;
	}

	public void setUserPass(String userPass) {
		this.userPass = userPass;
	}
	
	
}